//
// Created by madison on 2022/7/29.
//

#include "vector"
#include "set"

using namespace std;

class Solution {
public:
    // 方法一：位运算
    int singleNumber(vector<int> &nums) {
        int ret = 0;
        for (auto e: nums) {
            ret ^= e;
        }
        return ret;
    }

    // 使用集合Set解决
    int singleNumber1(vector<int> &nums) {
        set<int> set = {};
        for (auto e: nums) {
            if (set.contains(e)) {
                //如果添加失败，说明这个值
                //在集合Set中存在，我们要
                //把他给移除掉
                set.erase(e);
            } else {
                set.insert(e);
            }
        }
        for (auto e: set) {
            return e;
        }
    }
};

int main() {
    Solution solution;
    vector<int> nums = {4, 1, 2, 1, 2};
    printf("%d", solution.singleNumber1(nums));
    return 0;
}